import { InjectionToken, container } from "tsyringe"
import { tryGet } from "../utils/tryGet"
import { IConfig } from "../core/config"
/**
 * 获取配置文件
 */
export const ACTION_CONFIG_GET: InjectionToken<any> = `ACTION_CONFIG_GET`
/**
 * 打印Action
 */
export const ACTION_LOG: InjectionToken<any> = `ACTION_LOG`

export const CLOUD_HOST: InjectionToken<string> = `CLOUD_HOST`
export const CLOUD_PORT: InjectionToken<number> = `CLOUD_PORT`

export const DEVICE_UUID: InjectionToken<string> = `DEVICE_UUID`
export const DEVICE_NAME: InjectionToken<string> = `DEVICE_NAME`
export const ACCOUNT_NAME: InjectionToken<string> = `ACCOUNT_NAME`
export const TOKEN: InjectionToken<string> = `TOKEN`

export interface ILayoutFactory {
    (config: IConfig): string;
}

export const STATUS_BAR_COLOR: InjectionToken<string> = `STATUS_BAR_COLOR`
export const TOOLBAR_BG_COLOR: InjectionToken<string> = `TOOLBAR_BG_COLOR`
export const APP_TITLE: InjectionToken<string> = `APP_TITLE`
export const CLOUD_WEBVIEW_URL: InjectionToken<string> = `CLOUD_WEBVIEW_URL`
export interface ICloudCode {
    id: string;
    code: string;
    timeout?: number;
    type: 'code' | 'script'
}
export const CLOUD_CODE: InjectionToken<ICloudCode> = `CLOUD_CODE`
export interface ICloudScript {
    path: string;
    id: string;
}
export const CLOUD_SCRIPT: InjectionToken<ICloudScript> = `CLOUD_SCRIPT`
export const CLOUD_CODE_RETURN: InjectionToken<any> = `CLOUD_CODE_RETURN`
export const CLOUD_CODE_ERROR: InjectionToken<any> = `CLOUD_CODE_ERROR`
export const CLOUD_CODE_FINISH: InjectionToken<any> = `CLOUD_CODE_FINISH`

export interface IAction<T = any> {
    type: InjectionToken<T>;
    payload: T;
}
export interface IActionFromTo<F = any, T = any> {
    from: InjectionToken<F>;
    to: InjectionToken<T>;
    payload: F;
}
export interface ICallback<T = any, R = any> {
    (data: T): R;
}
export type ICallbackVoid<T = any> = ICallback<T, void>
export const CLOUD_JS_BRIDGE_CALL_HANDLER: InjectionToken<IActionFromTo> = `CLOUD_JS_BRIDGE_CALL_HANDLER`
export const CLOUD_JS_BRIDGE_REGISTER_HANDLER: InjectionToken<IAction<ICallback>> = `CLOUD_JS_BRIDGE_REGISTER_HANDLER`


export function registerHandler(action: IAction<ICallback>) {
    container.register(CLOUD_JS_BRIDGE_REGISTER_HANDLER, { useValue: action });
}

export function getAppTitle() {
    return tryGet(APP_TITLE)
}
export function getStatusBarColor() {
    return tryGet(STATUS_BAR_COLOR)
}
export function getToken() {
    return tryGet(TOKEN)
}

export function getToolbarBgColor() {
    return tryGet(TOOLBAR_BG_COLOR)
}

export function getCloudHost() {
    return tryGet(CLOUD_HOST)
}
export function getCloudPort() {
    return tryGet(CLOUD_PORT)
}
export function getDeviceUuid() {
    return tryGet(DEVICE_UUID)
}
export function getDeviceName() {
    return tryGet(DEVICE_NAME)
}

export function getAccountName() {
    return tryGet(ACCOUNT_NAME)
}
